package com.lebang.controller.api;

import com.lebang.entity.RequestHeaders;
import com.lebang.feign.AppInfoFeign;
import com.lebang.model.MoneyCategory;
import com.lebang.model.Whether;
import com.lebang.model.back.AdCallback;
import com.lebang.model.enums.buss.TaskCategory;
import com.lebang.service.AppCallBakService;
import com.lebang.service.MemberNoticeService;
import com.lebang.service.PointService;
import com.lebang.service.buss.MemberTradeService;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: hwq
 * @Date: 2021/1/4 10:38
 * @Version 1.0
 */
@RestController
@RequestMapping("/api/call/back")
public class CallBackApi {
    @Autowired
    private AppInfoFeign appInfoFeign;
    @Autowired
    private AppCallBakService appCallBakService;
    @Autowired
    private MemberNoticeService memberNoticeService;
    @Autowired
    private PointService pointService;
    @Autowired
    private MemberTradeService memberTradeService;

    /**
     * 微服务奖励回调。
     *
     * @param request
     * @return
     */
    @PostMapping("/order")
    public JsonData back(HttpServletRequest request, @RequestBody AdCallback adCallback) {
        TaskCategory category=TaskCategory.valueOf(adCallback.getCategory());
        if(category==null){
            return Callback.fail("2001", "未找到应用参数");
        }
        try {
            boolean appSecret = adCallback.checkSign();
            if (!appSecret) {
                return Callback.fail("2002", "签名错误");
            }
            boolean b = appCallBakService.checkExit(adCallback);
            if(b){
                return Callback.success("2000","接收成功");
            }
            //创建订单号
            appCallBakService.createOrder(adCallback);
            RequestHeaders headers=new RequestHeaders();
            headers.setAppStore(adCallback.getAppStore());
            headers.setIpAddr(adCallback.getIpAddress());
            headers.setVersion(adCallback.getVersion());
            headers.setImei(adCallback.getImei());
            //发奖
            switch (adCallback.getRewardType()){
                case 0:
                    memberTradeService.addMoney(adCallback.getMemberId(),adCallback.getMoney(),adCallback.getMsg()
                    ,adCallback.getMsg(),"",headers,null, new MoneyCategory(Whether.YES, category));
                    break;
                case 1:
                pointService.addPoint(adCallback.getMemberId(),adCallback.getMoney(),category,headers, adCallback.getMsg());
                    break;
                case 3:break;
                default:
            }
            //推送
            return Callback.success("2000","接收成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Callback.fail("2002", "签名错误");
        }

    }
}
